/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.jsrpc.bridge;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

/**
 *
 * @author neo
 */
public class ScriptTagHandler extends TagSupport {
    private static final long serialVersionUID = -4720265556917060135L;
    private String base = null;

    public String getBase() {
        return base;
    }

    public void setBase(String base) {
        this.base = base;
    }
    
    
    
    @Override
    public int doStartTag() throws JspException {
        JspWriter out = this.pageContext.getOut();
            String s = base;
            System.out.println("page.jsp[" + s + "]");
            String sp[] = s.split("/");
            System.out.println("size:" + sp.length);
            StringBuffer sf = new StringBuffer();
            for (int i = 2; i < sp.length; i++) {
                //System.out.println("["+ sp[i]+"]");
                sf.append("../");
            }
            String pageId = (String)pageContext.getAttribute(Key.PAGE_ID);
        if( pageContext.getSession().getAttribute(Key.REMOTE_REGISTER+"."+pageId) != null ) {
            try {
            out.println("<script language=\"JavaScript\" type=\"text/javascript\" src=\""+sf.toString()+"jsrpc.js\"></script>");
            } catch(Exception ex) {
                ex.printStackTrace();
            }
        }
        return EVAL_BODY_INCLUDE;
    }

    @Override
    public int doEndTag() throws JspException {
        return super.doEndTag();
    }
}
